﻿using System.Windows;
using System.Windows.Controls;

using VSTalk.Engine.Annotations;
using VSTalk.Engine.Core.EntitySession;
using VSTalk.Engine.Core.Feed.MessageState;
using VSTalk.Model;

namespace VSTalk.Engine.Controls
{
    public class InterlocutorLabel : Control
    {
        public Interlocutor Interlocutor
        {
            get { return (Interlocutor)GetValue(InterlocutorProperty); }
            set { SetValue(InterlocutorProperty, value); }
        }

        public IInterlocutorContext Context
        {
            get { return AppServices.Get<ISessionModel>().GetInterlocutorContext(Interlocutor); }
        }
        
        [UsedImplicitly]
        public UnreadMessageCounter Counter
        {
            get { return _counter ?? (_counter = CreateCounter()); }
        }

        private UnreadMessageCounter CreateCounter()
        {
            var stateManager = AppServices.Get<IMessageSateManager>();
            var counter = stateManager.GetCounter(Context.Interlocutor);
            return counter;
        }

        // Using a DependencyProperty as the backing store for Interlocutor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty InterlocutorProperty =
            DependencyProperty.Register("Interlocutor", typeof(Interlocutor), typeof(InterlocutorLabel), new PropertyMetadata(null));

        private UnreadMessageCounter _counter;

        static InterlocutorLabel()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(InterlocutorLabel), new FrameworkPropertyMetadata(typeof(InterlocutorLabel)));
        }
    }
}
